package gui;

import java.awt.Color;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;

import javax.swing.BorderFactory;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JSpinner;
import javax.swing.SpinnerNumberModel;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

import data.EchoTemplate;

@SuppressWarnings("serial")
public class EchoPanel extends JPanel implements ActionListener, ChangeListener {
	private JCheckBox chbSync;
	private JComboBox cmbTime;
	private JSpinner spnFeedback;
	private JLabel lblTime, lblSync, lblFeedback;
	private EchoTemplate template;
	private boolean init = true;
	
	public EchoPanel(Color fgColor, Color bgColor, Font font, EchoTemplate template) {
		super();
		this.template = template;
		readTemplate();
		this.setBackground(bgColor);
		this.setBorder(BorderFactory.createLineBorder(fgColor));
		this.setLayout(new GridBagLayout());
		
		lblTime = new JLabel("Time: ");
		lblTime.setForeground(fgColor);
		lblTime.setFont(font);
		
		lblSync = new JLabel("Sync: ");
		lblSync.setForeground(fgColor);
		lblSync.setFont(font);
		
		lblFeedback = new JLabel("Feedback: ");
		lblFeedback.setForeground(fgColor);
		lblFeedback.setFont(font);
		
		cmbTime = new JComboBox();
		cmbTime.setBackground(bgColor);
		cmbTime.setForeground(fgColor);
		cmbTime.setFont(font);
		cmbTime.addActionListener(this);
		int sync = template.getSync();
		updateTime(sync);
		if (sync == 1) cmbTime.setSelectedItem("1/" + template.getTime());
		else cmbTime.setSelectedItem("" + template.getTime());
		
		chbSync = new JCheckBox();
		chbSync.setBackground(bgColor);
		chbSync.setForeground(fgColor);
		chbSync.addActionListener(this);
		if (sync == 0) chbSync.setSelected(false);
		else chbSync.setSelected(true);
		
		spnFeedback = new JSpinner(new SpinnerNumberModel(template.getFeedback(), 0, 100, 1));
		spnFeedback.setForeground(fgColor);
		spnFeedback.setBackground(bgColor);
		spnFeedback.setFont(font);
		spnFeedback.addChangeListener(this);
		
		GridBagConstraints c = new GridBagConstraints();
		
		c.gridx = 0;
		c.gridy = 0;
		this.add(lblSync, c);
		
		c.gridx = 1;
		c.gridy = 0;
		this.add(chbSync, c);
		
		c.gridx = 0;
		c.gridy = 1;
		this.add(lblTime, c);
		
		c.gridx = 1;
		c.gridy = 1;
		this.add(cmbTime, c);
		
		c.gridx = 0;
		c.gridy = 2;
		this.add(lblFeedback, c);
		
		c.gridx = 1;
		c.gridy = 2;
		this.add(spnFeedback, c);
	}

	private void updateTime(int sync) {
		init = true;
		cmbTime.removeAllItems();
		if (sync == 0) {
			for (int i = 25; i < 20000; i++) cmbTime.addItem("" + i);		
		}
		else {
			int i = 1;
			while (i < 33) {
				cmbTime.addItem("1/" + i);
				i *= 2;
			}
		}
		init = false;
	}
	
	private void readTemplate() {
		File file = new File(System.getProperty("user.dir") + System.getProperty("file.separator") + "echo");
		if (file.exists()) {
			try {
				FileInputStream fis = new FileInputStream(file);
				template.deserialize(fis);
			} 
			catch (FileNotFoundException fnfe) {
				fnfe.printStackTrace();
			}
		}
	}
	
	private void writeTemplate() {
		File file = new File(System.getProperty("user.dir") + System.getProperty("file.separator") + "echo");
		try {
			FileOutputStream fos = new FileOutputStream(file);
			template.serialize(fos);
		} 
		catch (FileNotFoundException fnfe) {
			fnfe.printStackTrace();
		}
	}

	@Override
	public void actionPerformed(ActionEvent ae) {
		if (ae.getSource() == chbSync) {
			System.out.println("chbSync");
			if (template.getSync() == 0) {
				template.setSync(1);
				updateTime(1);
			}
			else {
				template.setSync(0);
				updateTime(0);
			}
			writeTemplate();
		}
		else if (ae.getSource() == cmbTime) {
			if (!init) {
				if (template.getSync() == 0) {
					template.setTime(Integer.parseInt((String) cmbTime.getSelectedItem())); 
				}
				else {
					String time = (String) cmbTime.getSelectedItem();
					template.setTime(Integer.parseInt(time.substring(time.indexOf("/") + 1)));
					System.out.println("" + Integer.parseInt(time.substring(time.indexOf("/") + 1)));
				}
				writeTemplate();
			}
		}
	}

	@Override
	public void stateChanged(ChangeEvent ce) {
		if (ce.getSource() == spnFeedback) {
			template.setFeedback((Integer) spnFeedback.getValue());
			writeTemplate();
		}
	}
}
